import java.io.*;
import java.util.*;

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    private static List<Employee> employees = Arrays.asList(
            new RegularEmployee("员工A", "E001", 0, 0),
            new RegularEmployee("员工B", "E002", 0, 0),
            new Manager("员工C", "M001", 0, 0),
            new Manager("员工D", "M002", 0, 0)
    );

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n=== 员工薪资管理系统 ===");
            System.out.println("1. 输入员工薪资信息");
            System.out.println("2. 查看员工薪资");
            System.out.println("3. 打印工资单");
            System.out.println("4. 存储工资单");
            System.out.println("5. 退出");
            System.out.print("请选择: ");

            switch (getIntInput()) {
                case 1: inputData(); break;
                case 2: viewSalary(); break;
                case 3: printPayroll(); break;
                case 4: savePayroll(); break;
                case 5:
                    System.out.println("再见！");
                    return;
                default: System.out.println("无效选择！");
            }
        }
    }

    private static void inputData() {
        System.out.println("\n=== 输入员工薪资信息 ===");
        for (Employee emp : employees) {
            System.out.printf("\n%s (%s):\n", emp.getName(), emp.getId());

            if (emp instanceof RegularEmployee) {
                inputRegularEmployee((RegularEmployee) emp);
            } else if (emp instanceof Manager) {
                inputManager((Manager) emp);
            }
        }
        System.out.println("\n输入完成！");
    }

    private static void inputRegularEmployee(RegularEmployee emp) {
        System.out.print("时薪: ");
        emp.setHourlyRate(getDoubleInput());

        // 工时输入带立即验证
        while (true) {
            System.out.print("工时: ");
            double hours = getDoubleInput();
            try {
                emp.setHoursWorked(hours);
                break; // 输入成功，退出循环
            } catch (WorkTimeException e) {
                System.out.println("错误: " + e.getMessage() + "，请重新输入！");
            }
        }
    }

    private static void inputManager(Manager emp) {
        System.out.print("月薪: ");
        emp.setMonthlySalary(getDoubleInput());

        // 奖金输入带立即验证
        while (true) {
            System.out.print("奖金: ");
            double bonus = getDoubleInput();
            try {
                emp.setBonus(bonus);
                break; // 输入成功，退出循环
            } catch (WorkTimeException e) {
                System.out.println("错误: " + e.getMessage() + "，请重新输入！");
            }
        }
    }

    private static void viewSalary() {
        System.out.println("\n1. 普通员工 2. 管理人员 3. 返回");
        System.out.print("请选择: ");

        switch (getIntInput()) {
            case 1: showSalaries("普通员工"); break;
            case 2: showSalaries("管理人员"); break;
            case 3: return;
            default: System.out.println("无效选择！");
        }
    }

    private static void showSalaries(String type) {
        System.out.printf("\n=== %s薪资 ===\n", type);
        for (Employee emp : employees) {
            if (emp.getPosition().equals(type)) {
                try {
                    System.out.printf("%s: %.2f元\n", emp.getName(), emp.calculateSalary());
                } catch (WorkTimeException e) {
                    System.out.printf("%s: 错误 - %s\n", emp.getName(), e.getMessage());
                }
            }
        }
    }

    private static void printPayroll() {
        Employee emp = selectEmployee();
        if (emp != null) System.out.println("\n" + emp.generatePayroll());
    }

    private static void savePayroll() {
        Employee emp = selectEmployee();
        if (emp != null) {
            System.out.print("文件路径: ");
            String path = scanner.nextLine();
            try (PrintWriter writer = new PrintWriter(new FileWriter(path))) {
                writer.write(emp.generatePayroll());
                System.out.println("已保存至: " + path);
            } catch (IOException e) {
                System.out.println("保存失败: " + e.getMessage());
            }
        }
    }

    private static Employee selectEmployee() {
        System.out.println("\n选择员工:");
        for (int i = 0; i < employees.size(); i++) {
            System.out.printf("%d. %s\n", i + 1, employees.get(i).getName());
        }
        System.out.print("请选择: ");

        int choice = getIntInput();
        if (choice >= 1 && choice <= employees.size()) return employees.get(choice - 1);
        System.out.println("无效选择！");
        return null;
    }

    private static double getDoubleInput() {
        while (true) {
            try {
                return Double.parseDouble(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.print("输入无效，请重新输入: ");
            }
        }
    }

    private static int getIntInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.print("输入无效，请重新输入: ");
            }
        }
    }
}